package kz.kstu.scantest.business.common.util.comparator;

import kz.kstu.scantest.business.common.helper.FormDataHelper;
import kz.kstu.scantest.business.entity.data.FormData;

import java.util.Comparator;

/**
 * @author Filipp_Stankevich
 *         created: 10.10.11 23:24
 */
public class FormDataErrorComparator implements Comparator<FormData> {

    private boolean isAscending;

    public FormDataErrorComparator() {
        this(true);
    }

    public FormDataErrorComparator(boolean isAscending) {
        this.isAscending = isAscending;
    }
    @Override
    public int compare(FormData o1, FormData o2) {
        if (o1 == null) {
            return o2 == null ? 0 : -1;
        }

        if (o2 == null) {
            return 1;
        }

        String firstObject = FormDataHelper.getFormDataErrorMessage(o1);
        String secondObject = FormDataHelper.getFormDataErrorMessage(o2);

        if (firstObject == null) {
            return secondObject == null ? 0 : -1;
        }

        if (secondObject == null) {
            return 1;
        }

        return (isAscending ? 1 : -1) * firstObject.compareTo(secondObject);
    }
}
